/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gem.controller;

import gem.model.core.*;
import gem.view.Viewer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author INVEST INTECH.COM
 */
public class AppController implements DepartmentListener, ActionListener
{
    DepartmentCatalogueModel model;
    java.util.ArrayList< Viewer > views;

    public void dataChanged()
    {
        for( Viewer v : views )
        {
            v.update();
        }
    }

    public void actionPerformed( ActionEvent ae )
    {
        
    }

    public java.util.Collection< Department > getDepartments()
    {
        try
        {
            return model.listDepartments( false );
        }
        catch( SQLException e )
        {
            e.printStackTrace();
        }
        
        return null;
    }
    public AppController( DepartmentCatalogueModel model )
    {
        this.model = model;
        model.addDepartmentListener(this);
        views = new ArrayList<Viewer>( 3 );
    }

    public void addViewer( Viewer v )
    {
        // when adding view, it has to be updated in order to retrieve copy of departments
        v.update();
        views.add(v);
    }

    public void removeViewer( Viewer v )
    {
        views.remove(v);
    }
}
